package org.xenon.vote.service;

import org.springframework.stereotype.Service;
import org.xenon.vote.dao.imp.LoginDao;
import org.xenon.vote.dao.imp.UserDao;
import org.xenon.vote.model.pojo.po.Login;
import org.xenon.vote.model.pojo.po.User;

@Service
public class LoginService {
    private final LoginDao loginDao;
    private final UserDao userDao;

    public LoginService(LoginDao loginDao, UserDao userDao) {
        this.loginDao = loginDao;
        this.userDao = userDao;
    }

    /**
     *
     * @param cookie 登录Cookie
     */
    public User check(String cookie) {
        Login res = loginDao.get(cookie);
        if (res == null) return null;
        return userDao.get(res.getMail());
    }

    public void logout(String mail){
        loginDao.del(mail);
    }

    /**
     * 记录登录状态
     */
    public void login(String  token, String mail){
        loginDao.add(new Login(token, mail));
    }
}
